package com.spring.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class MyWebApplicationInitializer implements WebApplicationInitializer {
    //配置
    //这个类一定得叫这个名 不然配置不生效
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //使用注解配置的时候就需要使用AnnotationConfig系列的上下文管理器
        //加载Appconfige的配置
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(Appconfig.class);
        ac.refresh();

        //创建和注册DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(ac);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app",servlet);
        registration.setLoadOnStartup(1);
//        registration.addMapping("*.do");
//        registration.addMapping("*.api");//“*.api”表示拦截所有后缀为api的请求， DispatcherServlet 进行解析，。
        registration.addMapping("/");
    }
}
